﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateOrderServiceCommand : ProviderQueueCommand<IProviderFactory, OrderRecord, ResponseRecord>
    {
        public UpdateOrderServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(OrderRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Record;
            var obj = new OrderInfo()
            {
                AcceptanceCode = record.AcceptanceCode,
                ArFlag = record.ArFlag,
                AuCode = record.AuCode,
                BilltoCode = record.BillCode,
                BilltoName = record.BillName,
                BookedDate = record.BookedDate,
                CancelFlag = record.CancelFlag.HasValue ? record.CancelFlag.Value.ToBooleanString() : null,
                Carrier = record.Carrier,
                City = record.City,
                CreatedBy = CONST_NAME.SYNCHRONIZE_ACCOUNT,
                CreationDate = DateTime.Now,
                DeliveryNo = record.DeliveryNumber,
                InvoiceDate = record.InvoiceDate,
                InvoiceNo = record.InvoiceNumber,
                InvOrg = record.InvoiceOrganization,
                LineNo = record.LineNumber,
                LineStatus = record.LineStatus,
                Model = record.Model,
                ModelSuffix = record.ModelSuffix,
                OrderAmt = record.OrderAmount,
                OrderNo = record.OrderNumber,
                OrderQty = record.OrderQuantity,
                OrderTypeName = record.OrderTypeName,
                PickNo = record.PickNumber,
                PickReleaseDate = record.PickReleaseDate,
                PickSeq = record.PickSequence,
                ProdDiv = record.ProductDivision,
                ReleaseQty = record.ReleaseQuantity,
                RequestArrivalDate = record.RequestArrivalDate,
                RequestShippedDate = record.RequestShippedDate,
                SaleschannelCode = record.SalesChannelCode,
                SaleschannelName = record.SalesChannelName,
                Salesperson = record.SalesPerson,
                ShippingMethod = record.ShippingMethod,
                ShippingRemark = record.ShippingReward,
                ShiptoCode = record.ShipCode,
                ShiptoName = record.ShipName,
                Subinventory = record.SubInventory,
                Suffix = record.Suffix,
                UniqueId = record.UniqueId,
                UnitSellPrice = record.UnitSellPrice,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE
            };
            var itemCheck = Providers.OrderInfo.Retrieve(record.OrderNumber, record.InvoiceNumber, record.LineNumber);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.OrderInfo.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.OrderInfo.Update(obj);
            }
            return true;
        }
    }
}
